package org.springframework.boot.netty.support;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.netty.exception.ConvertException;

import java.text.SimpleDateFormat;
import java.util.Map;

public class JsonUtil {



    private static ObjectMapper objectMapper = new ObjectMapper();
    static{
        //对象的所有字段全部列入
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);

        //取消默认转换timestamps形式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);

        //忽略空Bean转json的错误
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);

        //所有的日期格式都统一为以下的样式，即yyyy-MM-dd HH:mm:ss
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        //忽略 在json字符串中存在，但是在java对象中不存在对应属性的情况。防止错误
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,true);
    }



    public static <T> String obj2String(T obj){
        if(obj == null){
            return null;
        }

        if( BeanUtils.isSimpleValueType(obj.getClass()) && !(obj instanceof String)){
            return null;
        }

        try {
            return obj instanceof String ? (String)obj :  objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new ConvertException("can not converter for :"+obj);
        }
    }

    /**
     * 输出形式不同而已.
     * @param obj
     * @return
     */
    public static <T> String obj2StringPretty(T obj){
        if(obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String)obj :  objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (Exception e) {
            throw new ConvertException("can not converter for :"+obj);
        }
    }





    public static <T> T string2Obj(String str,Class<T> clazz){
        if(StringUtils.isEmpty(str) || clazz == null){
            return null;
        }
        try {
            return clazz.equals(String.class)? (T)str : objectMapper.readValue(str,clazz);
        } catch (Exception e) {
            throw new ConvertException("can not converter for :"+str);
        }
    }



    // List<User> userListObj1 = JsonUtil.string2Obj(userListStr, new TypeReference<List<User>>() {});
    public static <T> T string2Obj(String str, TypeReference<T> typeReference){
        if(StringUtils.isEmpty(str) || typeReference == null){
            return null;
        }
        try {
            return (T)(typeReference.getType().equals(String.class)? str : objectMapper.readValue(str,typeReference));
        } catch (Exception e) {
            throw new ConvertException("can not converter for :"+str);
        }
    }

     // List<User> userListObj2 = JsonUtil.string2Obj(userListStr,List.class,User.class);
    public static <T> T string2Obj(String str,Class<?> collectionClass,Class<?>... elementClasses){
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);
        try {
            return objectMapper.readValue(str,javaType);
        } catch (Exception e) {
            throw new ConvertException("can not converter for :"+str);
        }
    }

    // 将Bean转成Map
    public static Map<String, Object> bean2Map(Object obj) {
        try {
            return objectMapper.readValue(obj2String(obj), Map.class);
        }catch (Exception e) {
            throw new ConvertException("can not converter for :"+obj);
        }
    }

    // 将jsonString转成Map
    public static Map<String, Object> string2Map(String str) {
        try {
            return objectMapper.readValue(str, Map.class);
        }catch (Exception e) {
            throw new ConvertException("can not converter for :"+str);
        }
    }

}
